package edu.ilstu.mind.ise.ccp.config;

import java.util.Map;

/**
 * Utility class to parse a YAML configuration
 * 
 * @author Eric Thill
 *
 */

public class ConfigParserUtil {

	public static String getRequiredString(Map<String, Object> props, String key) {
		Object obj = props.get(key);
		if(obj == null) {
			throw new NullPointerException("<" + key + "> does not exist in props");
		}
		return obj.toString();
	}
	
	public static long getRequiredLong(Map<String, Object> props, String key) {
		return Long.parseLong(getRequiredString(props, key));
	}
	
	public static int getRequiredInt(Map<String, Object> props, String key) {
		return Integer.parseInt(getRequiredString(props, key));
	}
	
	public static short getRequiredShort(Map<String, Object> props, String key) {
		return Short.parseShort(getRequiredString(props, key));
	}
	
	public static byte getRequiredByte(Map<String, Object> props, String key) {
		return Byte.parseByte(getRequiredString(props, key));
	}
	

	public static String getString(Map<String, Object> props, String key) {
		Object obj = props.get(key);
		return obj != null ? obj.toString() : null;
	}
	
	public static String getString(Map<String, Object> props, String key, String defaultValue) {
		String value = getString(props, key);
		return value != null ? value : defaultValue;
	}
	
	public static long getLong(Map<String, Object> props, String key, long defaultValue) {
		String value = getString(props, key);
		return value != null ? Long.parseLong(value) : defaultValue;
	}
	
	public static int getInt(Map<String, Object> props, String key, int defaultValue) {
		String value = getString(props, key);
		return value != null ? Integer.parseInt(value) : defaultValue;
	}
	
	public static short getShort(Map<String, Object> props, String key, short defaultValue) {
		String value = getString(props, key);
		return value != null ? Short.parseShort(value) : defaultValue;
	}
	
	public static byte getByte(Map<String, Object> props, String key, byte defaultValue) {
		String value = getString(props, key);
		return value != null ? Byte.parseByte(value) : defaultValue;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> getSubmap(Map<String, Object> props, String key) {
		Object obj = props.get(key);
		if(obj == null) {
			throw new NullPointerException("<" + key + "> does not exist in props");
		}
		return (Map<String, Object>)obj;
	}
}
